<?php

class TopicextController extends Controller
{
    public $layout = '//layouts/main';
    public $defaultAction = 'manage';

    public function actionManage()
    {
        if (isset($_GET['id'])) {
            $model = Topic::model()->findByPk($_GET['id']);
            $model->searchKey = isset($_GET['Topic']['searchKey']) ? $_GET['Topic']['searchKey'] : '';
            $model->setScenario('search');
            $this->render('manage', array(
                'model' => $model,
            ));
            return;
        }
        Yii::app()->user->setFlash('alert-failure', "Link failure");
        $this->redirect(array($this->defaultAction));
    }


    public function actionDelete()
    {
        if (Yii::app()->request->isAjaxRequest) {
            $result = 'Success';
            $data = @file_get_contents("php://input");
            if ($data && $data = json_decode($data, true)) {
                $ids = $data['ids'];
                foreach ($ids as $id) {
                    if (($model = TopicExt::model()->findByPk($id)) && $model->delete()) {
                        $result = 'Success';
                    } else {
                        $result = 'failed';
                        break;
                    }
                }
            }
            echo $result;
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }

    }

    public function actionCreate()
    {
        $model = new Topic();
        if (isset($_POST['Topic'])) {
            $model->attributes = $_POST['Topic'];
            if ($model->save()) {
                Yii::app()->user->setFlash('alert-success', 'Create Success');
                $this->redirect($this->defaultAction);
            } else {
                Yii::app()->user->setFlash('alert-failure', 'Create failed');
            }
        }
        $this->render('model', array(
            'model' => $model,
        ));
    }

    public function actionAdd()
    {

        if (Yii::app()->request->isAjaxRequest) {
            $result = 'Success';
            $tid = $this->getQuery('get', 'tid', null);
            $id = $this->getQuery('get', 'id', null);
            $type = $this->getQuery('get', 'type', null);
            if ($type == TopicExt::TYPE_TASK) {
                if ($model = TopicExt::build($tid, $id, $type)) {
                    if ($model->save()) {
                        $result = 'Success';
                    } else {
                        $result = 'add failed';
                    }
                }
            } else {
                $data = @file_get_contents("php://input");
                if ($data && $data = json_decode($data, true)) {
                    $sort = $data['sort'];
                    $extids = $data['extids'];
                    if ($sort == 'up') {
                        $weight = TopicExt::getMaxWeight($tid);
                        $weight += sizeof($extids);
                    } else {
                        $weight = TopicExt::getMinWeight($tid);
                        $weight--;
                    }

                    foreach ($extids as $extid) {
                        if (($model = TopicExt::build($tid, $extid, $type,$weight)) && $model->save()) {
                            $result = 'Success';
                        }
                        $weight--;
                    }
                }
            }
            echo $result;
        } else {
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
        }


    }

    public function actionLinkType()
    {
        if (isset($_GET['id']) && isset($_GET['type'])) {
            $model = Topic::model()->findByPk($_GET['id']);
            $model->searchKey = isset($_GET['Topic']['searchKey']) ? $_GET['Topic']['searchKey'] : '';
            $model->taskId = isset($_GET['Topic']['taskId']) ? $_GET['Topic']['taskId'] : '';
            $model->setScenario('search');
            $type = $_GET['type'];
            $this->render('linktype', array(
                'model' => $model,
                'type' => $type
            ));
            return;
        }

        Yii::app()->user->setFlash('alert-failure', "Link failure");
        $this->redirect(array($this->defaultAction));
    }

    public function colReviseLink($data, $row, $c)
    {
        $name = $c->name;
        $isVideo = $data->isVideo();
        switch ($name) {
            case 'choose':
                return CHtml::checkBox('id', false, ['class' => 'ext', 'id' => $data->id]);
                break;
            case 'name':
                return $isVideo ? $data->Video->name : $data->Task->name;
                break;
            case 'count':
                return $isVideo ? 1 : sizeof($data->Task->Videos);
                break;
            case 'type':
                return $isVideo ? '视屏' : '采集任务';
                break;
            case 'operations':
                $params = [];
                $res = '';
                //delete
                $params[] = '删除';
                $params[] = Yii::app()->getController()->createUrl("delete", array("id" => $data->id,));
                $res .= '<a class ="muiltyDelete "  tooltip="delete" title="%s"  href="%s"> 删除</a>';
                return vsprintf($res, $params);
                break;
            default:
                return $data->$name;
        }
    }


}